<script setup>
import { RouterView } from 'vue-router'
import { onMounted, onUnmounted, computed } from 'vue'
import { useUserStore } from './stores/user'
import { useCartStore } from './stores/cart'

const userStore = useUserStore()
const cartStore = useCartStore()

// 获取购物车商品数量
const cartItemCount = computed(() => cartStore.totalCount)

// 登录状态变化处理函数
const handleLoginStatusChange = (isLoggedIn, userInfo) => {
  if (isLoggedIn && userInfo) {
    console.log('用户已登录:', userInfo.username)
    // 这里可以添加登录成功后的操作，比如显示欢迎消息、记录登录日志等
    // 例如：可以调用API记录登录事件、更新统计数据等
    // 也可以触发全局通知
    console.log('触发登录成功事件，可以在这里执行相关操作')
  } else {
    console.log('用户已登出')
    // 这里可以添加登出后的操作，比如清除临时数据、记录登出日志等
    console.log('触发登出事件，可以在这里执行相关清理操作')
  }
}

// 应用初始化时加载用户状态和购物车
onMounted(() => {
  userStore.loadFromLocalStorage()
  cartStore.loadCart()
  
  // 添加登录状态监听器
  userStore.addLoginListener(handleLoginStatusChange)
})

// 组件卸载时移除监听器，避免内存泄漏
onUnmounted(() => {
  userStore.removeLoginListener(handleLoginStatusChange)
})
</script>

<template>
  <!-- 根据路由的meta信息决定使用哪种布局 -->
  <template v-if="$route.meta.layout === 'auth'">
    <!-- 认证页面布局 (登录/注册) -->
    <RouterView />
  </template>
  <template v-else>
    <!-- 常规页面布局 -->
    <div class="app-container">
      <header class="app-header">
        <div class="header-content">
          <div class="logo-container">
            <RouterLink to="/" class="logo-link">
              <h1 class="app-title">购物商城</h1>
            </RouterLink>
          </div>
          
          <nav class="app-nav">
            <RouterLink to="/" class="nav-item" active-class="active">首页</RouterLink>
            <RouterLink to="/products" class="nav-item" active-class="active">商品列表</RouterLink>
            <RouterLink to="/about" class="nav-item" active-class="active">关于我们</RouterLink>
          </nav>
          
          <div class="user-actions">
            <RouterLink to="/cart" class="nav-item cart-link">
              <span class="cart-icon">🛒</span>
              <span v-if="cartItemCount > 0" class="cart-badge">{{ cartItemCount }}</span>
            </RouterLink>
            <template v-if="userStore.isLoggedIn">
              <RouterLink to="/user/profile" class="nav-item user-profile-link">
                <span class="avatar">
                  {{ userStore.userInfo.username ? userStore.userInfo.username.charAt(0).toUpperCase() : '?' }}
                </span>
                <span class="username">{{ userStore.userInfo.username }}</span>
              </RouterLink>
            </template>
            <template v-else>
              <RouterLink to="/login" class="nav-item">登录</RouterLink>
              <RouterLink to="/register" class="nav-item register-btn">注册</RouterLink>
            </template>
          </div>
        </div>
      </header>
      
      <main class="app-main">
        <div class="main-content">
          <RouterView />
        </div>
      </main>
      
      <footer class="app-footer">
        <div class="footer-content">
          <p>© 2024 购物商城. 保留所有权利.</p>
        </div>
      </footer>
    </div>
  </template>
</template>

<style scoped>
/* 应用容器 */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  width: 100%;
}

/* 头部样式 */
.app-header {
  background-color: #ffffff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 80px;
}

.logo-container {
  flex-shrink: 0;
}

.logo-link {
  text-decoration: none;
}

.app-title {
  margin: 0;
  font-size: 28px;
  color: #4285f4;
  font-weight: 700;
}

/* 导航样式 */
.app-nav {
  display: flex;
  gap: 32px;
  flex: 1;
  justify-content: center;
}

.nav-item {
  color: #666;
  text-decoration: none;
  font-size: 18px;
  padding: 10px 0;
  position: relative;
  transition: color 0.3s ease;
}

.nav-item:hover {
  color: #4285f4;
}

.nav-item.active {
  color: #4285f4;
}

.nav-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #4285f4;
  border-radius: 1px;
}

/* 用户操作 */
.user-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.cart-link {
  position: relative;
  display: flex;
  align-items: center;
  font-size: 24px;
}

.cart-icon {
  font-size: 24px;
}

.cart-badge {
  position: absolute;
  top: -8px;
  right: -12px;
  background-color: #ff4757;
  color: white;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
  font-weight: 600;
}

.user-profile-link {
  display: flex;
  align-items: center;
  gap: 8px;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: #4285f4;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 500;
}

.username {
  font-size: 14px;
}

.register-btn {
  background-color: #4285f4;
  color: white !important;
  padding: 6px 16px;
  border-radius: 6px;
  font-size: 14px;
}

.register-btn:hover {
  background-color: #3367d6;
}

/* 主内容区 */
.app-main {
  flex: 1;
  background-color: #f5f5f5;
  padding: 0;
  display: flex;
  justify-content: center;
}

.main-content {
  width: 100%;
  max-width: 1400px;
  padding: 30px;
}

/* 页脚样式 */
.app-footer {
  background-color: #ffffff;
  padding: 30px 0;
  border-top: 1px solid #eee;
}

.footer-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 30px;
  text-align: center;
}

.footer-content p {
  margin: 0;
  color: #999;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 1440px) {
  .header-content,
  .footer-content {
    max-width: 1200px;
  }
  
  .main-content {
    max-width: 1200px;
  }
}

@media (max-width: 1280px) {
  .header-content,
  .footer-content {
    max-width: 100%;
  }
  
  .main-content {
    max-width: 100%;
    padding: 25px;
  }
}

@media (max-width: 768px) {
  .header-content {
    padding: 0 16px;
    height: 70px;
  }
  
  .app-title {
    font-size: 24px;
  }
  
  .app-nav {
    display: none; /* 在移动端隐藏导航 */
  }
  
  .user-actions {
    gap: 12px;
  }
  
  .nav-item {
    font-size: 16px;
  }
  
  .username {
    display: none;
  }
  
  .main-content {
    padding: 20px 16px;
  }
  
  .footer-content {
    padding: 0 16px;
  }
}

@media (max-width: 480px) {
  .header-content {
    padding: 0 12px;
    height: 64px;
  }
  
  .app-title {
    font-size: 20px;
  }
  
  .main-content {
    padding: 16px 12px;
  }
}
</style>
